package com.oocl.servlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.oocl.pojo.User;
import com.oocl.service.UserService;
import com.oocl.service.impl.UserServiceImpl;
import com.oocl.util.DateUtil;

public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static int sizeThreshold = 20*2014;
	private static int fileMaxSize = 50*2014;//冯绍峰的
	private static String repositoryPath = "/repo/";
       
	@SuppressWarnings("static-access")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		DiskFileItemFactory factory = new DiskFileItemFactory(sizeThreshold, new File(this.getRealPath(repositoryPath)));
		ServletFileUpload fileUpload = new ServletFileUpload(factory);
		fileUpload.setHeaderEncoding("utf-8");
		fileUpload.setSizeMax(fileMaxSize);
		UserService userService = new UserServiceImpl();
		response.setContentType("text/html;charset=utf-8");
		
		if(fileUpload.isMultipartContent(request)){
			User user = new User();
			try {
				List<FileItem> items = fileUpload.parseRequest(request);
				for (FileItem item : items) {
					if(item.isFormField()){
						System.out.println(item.getFieldName()+":"+item.getString());
						String itemName = item.getFieldName();
						String value = item.getString("ISO-8859-1");
						if(null==value){
							response.sendRedirect("ErrorServlet?errorType=3");
						}
						if("username".equals(itemName)){
							if(null==value || "".equals(value)){
								response.sendRedirect("ErrorServlet?errorType=2");
								return;
							}
							System.out.println("suername=="+value);
							String uname = new String(value.getBytes("ISO-8859-1"),"UTF-8");
							System.out.println("suernamerr=="+uname);
							user.setUsername(uname);
						}else if("salary".equals(itemName)){
							try {
								double sal = Double.parseDouble(value);
								user.setSalary(sal);
							} catch (NumberFormatException e) {
								response.sendRedirect("ErrorServlet?errorType=4");
								return;
							}
						}else if("birthDate".equals(itemName)){
							try {
								Date birth = DateUtil.strToDate(value, "yyyy-MM-dd");
								user.setBirthDate(birth);
							} catch (ParseException e) {
								response.sendRedirect("ErrorServlet?errorType=5");
								return;
							}
						}else if("sex".equals(itemName)){
							if("0".equals(value)){
								user.setSex(false);
							}else {
								user.setSex(true);
							}
						}
					}else {
						System.out.println(item.getName());
						long size = item.getSize();
						if(size>fileMaxSize){
							response.sendRedirect("ErrorServlet?errorType=6");
							return;
						}
						InputStream in= item.getInputStream();
						byte[] buf = new byte[in.available()];
						in.read(buf);
						user.setHeadImage(buf);
					}
				}
				
			} catch (SizeLimitExceededException e) {
				response.sendRedirect("ErrorServlet?errorType=6");
				return;
			}catch (FileUploadException e) {
				e.printStackTrace();
			}
			System.out.println(user);
//			int m = userService.addUser(user);
//			if(m==1){
//				response.sendRedirect("success.html");
//				return;
//			}else {
//				response.getWriter().write("注册失败!");
//				return;
//			}
		}else {
			response.sendRedirect("ErrorServlet?errorType=1");
		}
		
	}
	
	private String getRealPath(String path){
		return this.getServletContext().getRealPath(path);
	}

}
